package cn.lili.controller.promotion;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.RedPackGoods;
import cn.lili.modules.promotion.entity.dto.search.RedPackGoodsSearchParams;
import cn.lili.modules.promotion.entity.vos.RedPackGoodsVO;
import cn.lili.modules.promotion.service.RedPackGoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 红包商品接口
 *
 * @author paulG
 * @since 2021/1/14
 **/
@RestController
@Api(tags = "红包商品接口")
@RequestMapping("/promotion/redPack")
@RequiredArgsConstructor
public class RedPackGoodsController {

    private final RedPackGoodsService redPackGoodsService;

    @PostMapping(consumes = "application/json", produces = "application/json")
    @ApiOperation(value = "添加红包商品")
    public ResultMessage<Object> addPointsGoods(@RequestBody List<RedPackGoods> redPackGoodsList) {
        if (redPackGoodsService.saveRedPackGoodsBatch(redPackGoodsList)) {
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.RED_PACK_GOODS_ERROR);
    }

    @PutMapping(consumes = "application/json", produces = "application/json")
    @ApiOperation(value = "修改红包商品")
    public ResultMessage<Object> updateRedPackGoods(@RequestBody RedPackGoodsVO redPackGoodsVO) {
        Objects.requireNonNull(UserContext.getCurrentUser());
        redPackGoodsService.updatePromotions(redPackGoodsVO);
        return ResultUtil.success();
    }

    @PutMapping("/status/{ids}")
    @ApiOperation(value = "修改红包商品状态")
    public ResultMessage<Object> updateRedPackGoodsStatus(@PathVariable String ids, Long startTime, Long endTime) {
        if (redPackGoodsService.updateStatus(Arrays.asList(ids.split(",")), startTime, endTime)) {
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.RED_PACK_GOODS_ERROR);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除红包商品")
    public ResultMessage<Object> delete(@PathVariable String ids) {
        if (redPackGoodsService.removePromotions(Arrays.asList(ids.split(",")))) {
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.RED_PACK_GOODS_ERROR);
    }

    @GetMapping
    @ApiOperation(value = "分页获取红包商品")
    public ResultMessage<Page<RedPackGoods>> getRedPackGoodsPage(RedPackGoodsSearchParams searchParams, PageVO page) {
        Page<RedPackGoods> redPackGoodsPage = redPackGoodsService.pageFindAll(searchParams, page);
        return ResultUtil.data(redPackGoodsPage);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取红包商品详情")
    public ResultMessage<Object> getRedPackGoodsDetail(@PathVariable String id) {
        RedPackGoodsVO redPackGoodsVO = redPackGoodsService.getRedPackGoodsDetail(id);
        return ResultUtil.data(redPackGoodsVO);
    }

}
